package com.huihui.activity.setting;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.huihui.R;
import com.huihui.base.BaseActivity;
import com.huihui.datamanager.AppManager;
import com.huihui.network.HttpContants;
import com.huihui.network.OkHttpRequest;
import com.huihui.network.base.CallBackUtil;
import com.huihui.network.pojo.UserInfo;
import com.huihui.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;


/**
 * 个人资料设置 -- 绑定手机号
 */
public class SettingBindPhoneActivity extends BaseActivity {

    private TextView bind_phone;
    private EditText edit_sms;
    private TextView get_sms;
    private EditText edit_phone;

    private boolean mIsPhoneValid;
    private int mCount = 60;


    private Runnable mCountDownRunnable = new Runnable() {
        @Override
        public void run() {
            if (mCount > 0) {
                get_sms.setText(getString(R.string.control_get_code_counter, mCount--));
                UIHandler.postDelayed(mCountDownRunnable, 1000);
            } else {
                mCount = 60;
                get_sms.setText(getString(R.string.get_sms));
                if (mIsPhoneValid) {
                    get_sms.setEnabled(true);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_bind_phone);
//        getSupportActionBar().setTitle(R.string.bind_phone);
        titleView.setText(R.string.bind_phone);
        initView();
        initListener();
    }

    private void initListener() {
        get_sms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取验证码
                doGetSms();
            }
        });

        bind_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //绑定
                doBindPhone();
            }
        });
    }

    private void doBindPhone() {
        if(getUserInfo() == null){
            return;
        }
        OkHttpRequest.getInstance().bindTelhone(HttpContants.bindTelhone,AppManager.getUserInfo().getUser_id(),edit_phone.getText().toString(),edit_sms.getText().toString(),new CallBackUtil.CallBackString(){

            @Override
            public void onFailure(Call call, Exception e) {

            }

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject json = new JSONObject(response);
                    if(json != null && json.has(HttpContants.DATA)){
                        UserInfo userInfo = AppManager.getGson().fromJson(json.getString(HttpContants.DATA),UserInfo.class);
                        if(userInfo != null){
                            AppManager.setUserInfo(userInfo);
                            setUserInfo(userInfo);
                            finish();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void doGetSms() {
        //获取验证码
        String inputPhone = edit_phone.getText().toString();
        if (!StringUtils.isPhone(inputPhone)) {
            Toast.makeText(getContext(),R.string.error_phone_not_match,Toast.LENGTH_SHORT).show();
            return;
        }
        get_sms.setEnabled(false);
        get_sms.setText(getString(R.string.control_get_code_counter, mCount--));
        //获取验证码请求
        OkHttpRequest.getInstance().sendSmsCode(HttpContants.sendSmsCode, AppManager.getUserInfo().getUser_id(), edit_phone.getText().toString(), 3, new CallBackUtil.CallBackString() {
            @Override
            public void onFailure(Call call, Exception e) {
                get_sms.setEnabled(true);
                mCount = 60;
                get_sms.setText(getString(R.string.get_sms));
                UIHandler.removeCallbacks(mCountDownRunnable);
            }

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject json = new JSONObject(response);
                    if(json.has(HttpContants.MSG)){
                        Toast.makeText(getContext(), json.getString(HttpContants.MSG),Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        UIHandler.postDelayed(mCountDownRunnable, 1000);
    }

    private void initView() {
        bind_phone = (TextView) findViewById(R.id.bind_phone);
        edit_sms = (EditText) findViewById(R.id.edit_sms);
        get_sms = (TextView) findViewById(R.id.get_sms);
        edit_phone = (EditText) findViewById(R.id.edit_phone);
    }
}
